<template>
	<view class="app">
		<view class="main">
			<view class="wxlogin">
				<view class="logo">
					<view class="posi-abs"></view>
					<image :src="config.logo"></image>
				</view>
				<view class="appname">{{config.name}}</view>
				<view class="subname">辅助管理工具</view>
				<view class="btn"><button @tap="wxauth" :disabled="!confisLoad">微信一键登录</button></view>
			</view>
		</view>
	</view>
</template>

<script>
	var that,App=getApp(),Comm=getApp().comm;
	export default{
		data(){
			return {
				config:{
					wx_config:{}
				},
				confisLoad:false,
				pagequery:{}
			}
		},
		methods:{
			wxauth(){
				console.log(this.config, this.pagequery);
				const backurl = this.pagequery.redirect_uri?this.pagequery.redirect_uri:location.protocol + '//' + location.host + location.pathname + '#/';
				const redirect_uri=location.protocol + '//' + location.host + location.pathname + location.hash + '?redirect_uri=' + encodeURIComponent(backurl);
				const redirect_url2 = Comm.servDomain  + '/api/wechat/jump?url=' + encodeURIComponent(redirect_uri);
				const url=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.config.wx_config.oa_app_id}&redirect_uri=${encodeURIComponent(redirect_url2)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
				location.href=url;
			},
			wxlogin(code){
				const data={
					code,
					pid:uni.getStorageSync('pid') || 0
				}
				Comm.http('/api/wechat/masthertoollogin',data,'POST').then(res=>{
					if(res.code==0){
						uni.showToast({
							icon:'error',
							title:res.msg,
							duration:1500,
							mask:true
						});
						setTimeout(()=>{
							uni.navigateTo({
								url:'/pages/index/login'
							});
						},1200);
						
						return;
					}
					if(res.code==1){
						if(res.data.code==2){
							//登录 成功
							uni.setStorageSync('login',{
								'openid':res.data.openid,
								'token':res.data.token
							});
							location.href=that.pagequery.redirect_uri;
						}
					}
				}).catch(err=>{
					setTimeout(()=>{
						uni.navigateTo({
							url:'/pages/index/login'
						});
					},1200);
				});
			},
			loadConfig(){
				return new Promise((resolve,reject)=>{
					Comm.http('/api/index/shareConfig').then(res=>{
						resolve(res.data);
					}).catch(e=>{
						reject(e);
					});
				});
			}
		},
		onLoad(e) {

			that=this;
			
			this.loadConfig().then(res=>{
				that.confisLoad=true;
				that.config=res;
				that.config.wx_config=JSON.parse(res.wx_config);
			});
			
			this.pagequery = e;
			if(e.code){
				this.wxlogin(e.code);
				return;
			}
			
			const login=uni.getStorageSync('login');
			if(login.token){
				uni.navigateTo({
					url:'/pages/index'
				});
				return;
			}
		}
	}
</script>

<style>
.main{
	height:100vh;
}
.wxlogin{
	display: flex; flex-direction: column; align-items: center; align-content: center; font-size: 12px; padding-top: 25px; line-height: 25px;
}
.wxlogin .appname{
	font-size: 20px; font-weight: bold;
}
.wxlogin .logo{
	 position: relative;
}
.wxlogin .logo .posi-abs{
	    width: 32px;
	    height: 6px;
	    opacity: 0.3;
	    background: rgb(76, 184, 144);
	    border-radius: 50%;
	    filter: blur(1px);
	    bottom: 3px;
		position: absolute;
		left: 50%; 
		transform: translateX(-50%);
}
.wxlogin .logo image{
	width: 58px; height: 58px; border-radius: 50%; 
	position: relative;
}
.btn {
	margin: 60px 0 0;
}
.btn button{
	height: 37px; line-height: 37px; font-size: 13px; width: 70vw; border: none;
	background-color: rgb(56, 152, 124); color: #fff; border-radius: 6px;
}
.agreement{
	margin-top: 17px; font-size: 11px;
}
.agreement text{
	color: rgb(56, 152, 124);
}
.fortel{
	position: absolute; bottom: 0; display: flex; flex-direction: column; align-items: center; align-content: center;
}
.fortel image{
	width: 40px; height: 40px; border-radius: 50%;
}
.fortel text{
	padding: 2px 0 12px; font-size: 11px;
}
.bindcard{
	display: flex; flex-direction: column; align-items: center; align-content: center; font-size: 12px; line-height: 25px; padding: 30px 47px 0px;
}
.bindcard .appname{
	font-size: 20px; font-weight: bold;
}
.bindcard .subname{
	margin-bottom: 35px;
}
.bindcard .bindinfo{
    display: flex;
    justify-content: center;
    align-items: center;
	height: 59px;
	font-size: 13px;
	width: 100%;
}
.bindcard .bindinfo .areacode{
	font-size: 16p; font-weight: bold;
}
.bindcard .bindinfo .filed1{
	width: 55px; min-width: 55px;
}
.bindcard .bindinfo .filed2{
	flex: 1;
}
.bindcard input{
	font-size: 11px; height: 59px; flex: 1;
}
.bindcard .item2{
	border: rgba(0,0,0,0.08) 1px solid;
	border-left: none; border-right: none;
}
.bindcard .bindinfo .filed3{
	color: rgb(56, 152, 124);
}
.bindcard .logo{
	  margin-bottom: 35px; position: relative; display: flex; flex-direction: column; align-items: center; align-content: center; width: 100%;
}
.bindcard .logo image{
	width: 58px; height: 58px; border-radius: 50%; 
	position: relative;
}
.bindcard .logo .posi-abs{
	    width: 32px;
	    height: 6px;
	    opacity: 0.3;
	    background: rgb(76, 184, 144);
	    border-radius: 50%;
	    filter: blur(1px);
	    bottom: -4px;
		position: absolute;
		left: 50%; 
		transform: translateX(-50%);
}
</style>